//
//  BYSearchViewController.m
//  BYControls
//
//  Created by 彬boy on 2018/11/19.
//  Copyright © 2018年 彬boy. All rights reserved.
//

#import "BYSearchViewController.h"
#import "BYSearchController.h"

@interface BYSearchViewController ()<UISearchBarDelegate>

@property (nonatomic, strong) BYSearchController *ohSearchController;

@end

@implementation BYSearchViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.by_navBarControl.backgroundColor = [UIColor redColor];
    
    UIImageView *leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search"]];
    leftView.bounds = CGRectMake(0, 0, 24, 24);
    self.ohSearchController = [[BYSearchController alloc] initWithSearchResultsController:self
                                                                           searchBarFrame:CGRectMake(0, 0, kScreenH, 44)
                                                                              placeholder:@"请输入搜索内容进行搜索"
                                                                        textFieldLeftView:leftView
                                                                         showCancelButton:YES
                                                                             barTintColor:[UIColor blueColor]];
    
    [self.ohSearchController.ohSearchBar becomeFirstResponder];
    self.ohSearchController.ohSearchBar.delegate = self;
    [self.ohSearchController.ohSearchBar setLeftPlaceholder];
    self.by_navBarControl.navTitleView = self.ohSearchController.ohSearchBar;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self.ohSearchController.ohSearchBar resignFirstResponder];
    // 让取消按钮一直处于激活状态
    UIButton *cancelBtn = [searchBar valueForKey:@"cancelButton"];
    cancelBtn.enabled = YES;
}

@end
